package org.lbm.flow.agent.impl;

import org.lbm.flow.agent.Agent;
import org.lbm.flow.agent.AgentResult;
import org.lbm.flow.context.FlowContext;

import java.util.concurrent.ThreadLocalRandom;

/**
 * 简单智能体实现
 *
 * @author insist
 * @since 2025-08-01
 * 用于演示和测试
 */
public class SimpleAgent implements Agent {

    private final String id;
    private final String name;
    private final String description;
    private final AgentBehavior behavior;

    public enum AgentBehavior {
        SUCCESS,        // 总是成功
        FAILURE,        // 总是失败
        RANDOM,         // 随机成功或失败
        DELAY,          // 延迟后成功
        EXCEPTION       // 抛出异常
    }

    public SimpleAgent(String id, String name, String description) {
        this(id, name, description, AgentBehavior.SUCCESS);
    }

    public SimpleAgent(String id, String name, String description, AgentBehavior behavior) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.behavior = behavior;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public AgentResult execute(FlowContext context) {
        System.out.println("智能体 " + name + " 开始执行...");

        try {
            switch (behavior) {
                case SUCCESS:
                    return executeSuccess(context);
                case FAILURE:
                    return executeFailure(context);
                case RANDOM:
                    return executeRandom(context);
                case DELAY:
                    return executeWithDelay(context);
                case EXCEPTION:
                    return executeWithException(context);
                default:
                    return AgentResult.failed("未知的行为类型: " + behavior);
            }
        } catch (Exception e) {
            return AgentResult.failed("执行异常: " + e.getMessage(), e);
        }
    }

    private AgentResult executeSuccess(FlowContext context) {
        // 从上下文获取输入数据
        String input = context.getVariable("input", "默认输入");

        // 模拟处理逻辑
        String output = "处理结果: " + input + " (由 " + name + " 处理)";

        // 将结果存储到上下文
        context.setVariable("output_" + id, output);
        context.setVariable("processed_by", name);

        return AgentResult.success(output, "智能体 " + name + " 执行成功");
    }

    private AgentResult executeFailure(FlowContext context) {
        return AgentResult.failed("智能体 " + name + " 模拟失败");
    }

    private AgentResult executeRandom(FlowContext context) {
        boolean success = ThreadLocalRandom.current().nextBoolean();

        if (success) {
            return executeSuccess(context);
        } else {
            return executeFailure(context);
        }
    }

    private AgentResult executeWithDelay(FlowContext context) {
        try {
            // 模拟延迟处理
            long delay = ThreadLocalRandom.current().nextLong(1000, 3000);
            System.out.println("智能体 " + name + " 延迟 " + delay + "ms");
            Thread.sleep(delay);

            return executeSuccess(context);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return AgentResult.failed("执行被中断", e);
        }
    }

    private AgentResult executeWithException(FlowContext context) {
        throw new RuntimeException("智能体 " + name + " 模拟异常");
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    public String getType() {
        return "SimpleAgent";
    }

    public AgentBehavior getBehavior() {
        return behavior;
    }

    @Override
    public String toString() {
        return String.format("SimpleAgent{id='%s', name='%s', behavior=%s}",
                           id, name, behavior);
    }
}
